```

library(tidyverse)
## ── Attaching packages ───── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.0     ✓ purrr   0.3.3
## ✓ tibble  2.1.3     ✓ dplyr   0.8.5
## ✓ tidyr   1.0.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ── Conflicts ──────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

Load Files

SNPs<- read.table("23andMe_complete.txt", header = TRUE, sep = "\t")

To adjust figure size {r, fig.width = 6, fig.height = 6}

SNPs$chromosome = ordered(SNPs$chromosome, levels=c(seq(1, 22), "X", "Y", "MT"))
ggplot(data = SNPs) + 
  geom_bar(mapping = aes(x = genotype, fill = chromosome)) + 
  coord_polar() +
  ggtitle("Total SNPs for each genotype") +
  ylab("Total number of SNPs") +
  xlab("Genotype")

Plot graph to a pdf outputfile

pdf("SNP_example_plot.pdf", width=6, height=3)
ggplot(data = SNPs) + 
  geom_bar(mapping = aes(x = chromosome, fill = genotype))
dev.off()
## quartz_off_screen 
##                 2

Plot graph to a png outputfile

ppi <- 300
png("SNP_example_plot.png", width=6*ppi, height=6*ppi, res=ppi)
ggplot(data = SNPs) + 
  geom_bar(mapping = aes(x = chromosome, fill = genotype))
dev.off()
## quartz_off_screen 
##                 2
Genotype counts per chromosome

Genotype counts per chromosome

library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
p <- ggplot(data = SNPs) + 
  geom_bar(mapping = aes(x = genotype, fill = chromosome))

ggplotly(p)

Excercise 1

ggplot(SNPs, aes(x=chromosome)) +
  geom_bar(fill = "#6666CC", colour = "black") + 
  labs(title = "Total SNP Count for Each chromosome")

Exercise 2

mycolors <-c("AA"= "red","AC"= "red", "AG"= "red", "AT"= "red", "CC"= "red", "CG"= "red", "CT"= "red", "DD"= "red", "DI"= "red", "GG"= "red", "GT"= "red", "II"= "red", "TT"= "red", "A" = "orange", "C" = "orange", "G" = "orange", "T" = "orange", "I" = "yellow", "D" = "yellow")

ggplot(SNPs, aes(x=chromosome, fill =  genotype)) +
  geom_bar() +
  scale_fill_manual(values=c("AA"= "red","AC"= "red", "AG"= "red", "AT"= "red", "CC"= "red", "CG"= "red", "CT"= "red", "DD"= "red", "DI"= "red", "GG"= "red", "GT"= "red", "II"= "red", "TT"= "red", "A" = "orange", "C" = "orange", "G" = "orange", "T" = "orange", "I" = "green", "D" = "green", "--" = "green"))

Exercise 3

ppi <- 300
png("exercise3graph.png", width=6*ppi, height=6*ppi, res=ppi)
ggplot(SNPs, aes(x=chromosome, fill =  genotype)) +
  geom_bar(position = "dodge")
pdf("exercise3graph.pdf", width=6, height=3)
ggplot(SNPs, aes(x=chromosome, fill =  genotype)) +
  geom_bar(position = "dodge")
dev.off()
## quartz_off_screen 
##                 2
Individual Genotypes

Individual Genotypes

Exercise 4

ppi <- 2000
png("lab3_ex6.png", width=20*ppi, height=10*ppi, res=ppi)
ggplot(SNPs, aes(x=chromosome, fill =  genotype)) +
  geom_bar(position = "dodge") +
  facet_wrap(~genotype) +
  labs(title = "Lab3 ex6") +
  theme(axis.title.x = element_text(face="bold", colour="#990000", size=20))
Individual Genotypes

Individual Genotypes

Exercise 5

# Version 2
library(plotly)
ggplotly(
  ggplot(SNPs, aes(x=chromosome, fill =  genotype)) +
  geom_bar(position = "dodge") +
  facet_wrap(~genotype))

Exercise 6

library(DT)
SNPs_e.g.<- read.table("23andMe_example_cat25.txt", header = TRUE, sep = "\t")
datatable(SNPs_e.g.)